// 新房本地缓存数据

const _prefix = 'xinfang_'

/**
 * 获取搜索历史
 */
export function getSearchHistory() {
    
    return uni.getStorageSync(_prefix + 'search_history');
}

/**
 * 添加搜索历史
 */
export function addSearchHistory(data) {
    let search_history = uni.getStorageSync(_prefix + 'search_history') || []
	
	search_history.forEach((item, index) => {
		if(item.type == data.type && item.keywords == data.keywords){
			search_history.splice(index, 1)
		}
	})
	
	search_history.unshift(data)
    uni.setStorageSync(_prefix + 'search_history', search_history);
}

/**
 * 删除搜索历史
 */
export function deleteSearchHistory(index = false) {
	let search_history = uni.getStorageSync(_prefix + 'search_history') || []
	if(index !== false){
		search_history.splice(index, 1)
		uni.setStorageSync(_prefix + 'search_history', search_history);
	}else{
		uni.setStorageSync(_prefix + 'search_history', []);
	}
}

export default {
    getSearchHistory,
	addSearchHistory,
	deleteSearchHistory
}